<template>
	<div class="index">
    <el-dialog :title="tit" :visible.sync="log" :close-on-click-modal="false" width="440px">
      <div style="padding: 0 30px;">

        <div style="position: relative; margin-bottom: 20px; width: 340px;" v-if="(tit!='修改绑定手机号' && tit!='绑定手机号') || (xiugaishenme == 'mima' && jizhu) || xiugaishenme == 'real_name'">
          <el-input v-model="all.mobile" disabled></el-input>
        </div>

        <div style="position: relative; margin-bottom: 20px; width: 340px;" v-if="tit=='绑定手机号'">
          <el-input v-model="all.mobile" placeholder="输入手机号"></el-input>
        </div>

        <div v-if="tit=='修改绑定手机号'" style="margin-bottom: 20px; color: #888;">原手机号：{{all.mobile}}</div>
        <div style="position: relative; margin-bottom: 20px; width: 340px;" v-if="tit=='修改绑定手机号'">
          <el-input v-model="all.new_mobile" placeholder="输入新手机号"></el-input>
        </div>

        <div style="margin-bottom: 20px; position: relative; width: 340px;" v-if="tit=='绑定手机号'||tit=='修改绑定手机号'||!jizhu||tit=='修改使用者'">
          <el-input v-model="all.sms_code" placeholder="输入验证码"></el-input>
          <el-button style=" position: absolute; right: 0px; top: 0px;" type="danger" @click="getcode" :disabled="jinyong">{{tishi}}</el-button>
        </div>

        <div style="margin-bottom: 20px;" v-if="xiugaishenme == 'mima' && jizhu">
          <el-input v-model="all.yuanmima" show-password placeholder="输入原密码"></el-input>
        </div>

        <div style="color: #409eff; margin: 20px 0; cursor: pointer;" v-if="xiugaishenme == 'mima' && jizhu" @click="jizhu = false">忘记原密码，手机号验证码修改</div>

        <div style="color: #409eff; margin: 20px 0; cursor: pointer;" v-if="xiugaishenme == 'mima' && !jizhu" @click="jizhu = true">原密码修改</div>

        <div style="margin-bottom: 20px;" v-if="xiugaishenme == 'mima'">
          <el-input v-model="all.xinmima1" show-password placeholder="输入新密码"></el-input>
        </div>

        <div style="margin-bottom: 20px;" v-if="xiugaishenme == 'mima'">
          <el-input v-model="all.xinmima2" show-password placeholder="重复输入新密码"></el-input>
        </div>

        <div style="margin-bottom: 20px;" v-if="xiugaishenme == 'real_name'">
          <el-input v-model="all.real_name" placeholder="输入使用者"></el-input>
        </div>

      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="xiugaishiyongzhe" v-if="xiugaishenme == 'real_name'">确定
        </el-button>
        <el-button type="primary" @click="xiugaishoujihao" v-if="xiugaishenme == 'shoujihao'">确定
        </el-button>
        <el-button type="primary" @click="xiugaimima" v-if="xiugaishenme == 'mima'">确定
        </el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import axios from 'axios'
  export default {
    name: "xiugai",
    props:['xiugaishenme'],
    data() {
      return {
        jizhu:true,
        tit:"",
        log:true,
        all:{
          mobile:"",
          name:"",
          real_name:"",
          new_mobile:"",
          yuanmima:"",
          xinmima1:"",
          xinmima2:"",
          sms_code:"",
          id:""
        },
        count: 60,
        tishi: "获取验证码",
        jinyong: false
      }
    },
    created() {
      this.chushihua()
    },
    watch: {
  　　　xiugaishenme () {
  　　　 this.chushihua()
  　　 }
    },
    mounted() {

    },
    methods: {
      chushihua:function(){
        axios.get("/api/auth/me")
          .then( response=> {
            if (response.data.msg.code == 0) {

              this.all.name = response.data.data.name
              this.all.mobile = response.data.data.mobile
              this.all.real_name = response.data.data.real_name
              this.all.id = response.data.data.id

              console.log(this.xiugaishenme)
              if(this.xiugaishenme == 'shoujihao'){
                if(this.all.mobile == null || this.all.mobile == ''){
                  this.tit = '绑定手机号'
                }else{
                  this.tit = '修改绑定手机号'
                }
              }

              if(this.xiugaishenme == 'mima'){
                this.tit = '修改密码'
              }

              if(this.xiugaishenme == 'real_name'){
                this.tit = '修改使用者'
              }

            }else {
              this.$message.error(response.data.msg.info);
            }
          })
      },
      xiugaishiyongzhe:function(){
        if(this.all.real_name == ''){
          this.$message.error('请先输入使用者');
          return false
        }
        axios.post('/api/plan-market/user/info', {
            sms_code: this.all.sms_code,
            real_name: this.all.real_name
          })
          .then(response => {
            if (response.data.msg.code == 0) {
              this.$message({
                type: 'success',
                message: '修改信息成功!'
              });
              this.$emit('xiugaiwanbi')
            } else {
              this.$message.error(response.data.msg.info);
            }
          })
      },
      xiugaishoujihao:function(){
        let mobile = this.all.mobile

        if( this.tit == '修改绑定手机号'){
          mobile = this.all.new_mobile
          if (mobile == "") {
            this.$message.error('请先输入新手机号');
            return false
          }

        }else{
          if (mobile == "") {
            this.$message.error('请先输入手机号');
            return false
          }
        }

        axios.post('/api/plan-market/user/info', {
            sms_code: this.all.sms_code,
            mobile: mobile
          })
          .then(response => {
            if (response.data.msg.code == 0) {
              this.$message({
                type: 'success',
                message: '修改信息成功!'
              });
              this.$emit('xiugaiwanbi')
            } else {
              this.$message.error(response.data.msg.info);
            }
          })
      },
      xiugaimima:function(){
        if(this.jizhu){

          if(!this.all.yuanmima){
            this.$message.error('请输入原密码');
            return false
          }

          if(!this.all.xinmima1){
            this.$message.error('请输入新密码');
            return false
          }

          if(!this.all.xinmima2){
            this.$message.error('请再次输入新密码');
            return false
          }

          if(this.all.xinmima1 != this.all.xinmima2){
            this.$message.error('两次输入的密码不相同');
            return false
          }

          axios.post('/api/plan-market/new/password/reset', {
              original_password:this.all.yuanmima,
              password: this.all.xinmima1,
              id:this.all.id
            })
            .then(response => {
              if (response.data.msg.code == 0) {
                this.$message({
                  type: 'success',
                  message: '修改信息成功!'
                });
                this.$emit('xiugaiwanbi')
              } else {
                this.$message.error(response.data.msg.info);
              }
            })

        }else{

          if(!this.all.sms_code){
            this.$message.error('请输入验证码');
            return false
          }

          if(!this.all.xinmima1){
            this.$message.error('请输入新密码');
            return false
          }

          if(!this.all.xinmima2){
            this.$message.error('请再次输入新密码');
            return false
          }

          if(this.all.xinmima1 != this.all.xinmima2){
            this.$message.error('两次输入的密码不相同');
            return false
          }

          axios.post('/api/plan-market/password/reset', {
              sms_code: this.all.sms_code,
              // mobile: this.all.mobile,
              password: this.all.xinmima1,
              id:this.all.id
            })
            .then(response => {
              if (response.data.msg.code == 0) {
                this.$message({
                  type: 'success',
                  message: '修改信息成功!'
                });
                this.$emit('xiugaiwanbi')
              } else {
                this.$message.error(response.data.msg.info);
              }
            })
        }

      },
      //获取验证码

      getcode: function() {

        let mobile = this.all.mobile

        if( this.tit == '修改绑定手机号'){
          mobile = this.all.new_mobile
          if (mobile == "") {
            this.$message.error('请先输入新手机号');
            return false
          }

        }else{
          if (mobile == "") {
            this.$message.error('请先输入手机号');
            return false
          }
        }

        axios.post('/api/auth/sms_code', {mobile:mobile})
          .then(response => {
            if (response.data.msg.code == 0) {
              this.$message({
                message: '验证码已发送！',
                type: 'success'
              });
              this.daojishi()
            } else {
              this.$message.error(response.data.msg.info);
            }
          })

      },

      //倒计时功能
      daojishi: function() {
        var interval;
        interval = window.setInterval(() => {
          if (this.count <= 0) {
            window.clearInterval(interval)
            this.tishi = "获取验证码",
            this.jinyong = false
            this.count = 60
          } else {
            this.count -= 1;
            this.tishi = this.count + "秒重新获取"
            this.jinyong = true
          }
        }, 1000);
      },

      fixmima: function() {
        axios.post('/api/plan-market/password/reset', {
            id: this.form.id,
            password: this.form.pass
          })
          .then(response => {
            if (response.data.msg.code == 0) {
              this.form.pass = ""
              this.$message({
                type: 'success',
                message: '修改信息成功!'
              });
              this.chushihua()
            } else {
              this.$message.error(response.data.msg.info);
            }
          })
      },
      //修改账户信息
      fixpersongo: function() {
        var that = this
        axios.post('/api/plan-market/user/info', {
            sms_code: this.code,
            com: this.gongsiming
          })
          .then(response => {
            if (response.data.msg.code == 0) {
              this.$message({
                type: 'success',
                message: '修改信息成功!'
              });
              this.log = false
              this.chushihua()
            } else {
              this.$message.error(response.data.msg.info);
            }
          })
      },

      fixpersongo2: function() {

        var that = this

        axios.post('/api/plan-market/user/info', {

            sms_code: this.code,

            mobile: this.shoujihao

          })

          .then(response => {

            if (response.data.msg.code == 0) {

              this.$message({

                type: 'success',

                message: '修改信息成功!'

              });

              this.log = false

              this.chushihua()

            } else {

              this.$message.error(response.data.msg.info);

            }

          })

      },

      fixpersongo3: function() {

        var that = this

        if (this.mima != this.mima2) {

          this.$message.error('两次密码输入不同');

          return false

        }

        axios.post('/api/plan-market/shop/pwd/reset', {

            sms_code: this.code,

            password: this.mima,

            name: this.form.name,

            mobile: this.form.mobilezhen

          })

          .then(response => {

            if (response.data.msg.code == 0) {

              this.$message({

                type: 'success',

                message: '修改信息成功!'

              });

              this.log = false

              this.chushihua()

            } else {

              this.$message.error(response.data.msg.info);

            }

          })

      },
    }
  }
</script>

<style lang="scss" scoped>
  .index {
    /deep/ .el-input__inner{ width: 340px;}
  }
</style>
